#HEALTH OF ONLINE COMMUNITIES
#
#
#Obtain duration, longevity, and date information from data.
#
#
#Outputs a file with tab separated data in the form below:
#Username, date of first edit, last edit date, time between those dates (days), the longest time between edits, total amount of time spent editing,
#	amount of time editing in first month, and first session edit time 


import sys
import datetime


def main():
	
	getInfo()

def getInfo():
	user = None
	firstUser = 0

	for line in sys.stdin:
		pieces = line.split('\t')
		userid = pieces[0]
		start = convertTime(pieces[1].replace('.000Z',''))
		end = convertTime(pieces[2].replace('.000Z',''))
		duration = int(pieces[3])

		#if this user is the same as before, keep adding data
		if userid == user:
			totalDuration += duration
			date = start
			dateList.append(date)
			intersession = dateList[-1] - dateList[-2] #the amount of time between edits

			if (dateList[-1] - dateList[0]).days < 31: #calculates duration of just the first month
				firstMonthDuration += duration
			if intersession > maxIntersession: #finds the longest time between sessions
				maxIntersession = intersession
		else:
			if firstUser == 1: #used to skip the first iteration when there is no user to print info for
				longevity = (dateList[-1] - dateList[0]).days
				print '\t'.join([user, str(dateList[0].date()),str(dateList[-1].date()), str(longevity), str(maxIntersession.days), str(totalDuration), str(firstMonthDuration),str(firstDuration)])

			user = userid #changes current user
			totalDuration = duration #starts totalDuration at the duration of their first edit
			dateList = [start]
			firstMonthDuration = duration
			longevity = 0
			maxIntersession = datetime.timedelta(0)
			firstUser = 1
			firstDuration = duration #saves the first edit duration
				
	print '\t'.join([user, str(dateList[0].date()),str(dateList[-1].date()), str(longevity), str(maxIntersession.days), str(totalDuration), str(firstMonthDuration)])

#converts UTC time into datetime
def convertTime(input):
	times = input.split('T')
        date = times[0]
        time = times[1]
        
        dateOb = date.split('-')
        year = int(dateOb[0])
        month = int(dateOb[1])
        day = int(dateOb[2])

        timeOb = time.split(':')
	hour = int(timeOb[0])
        minute = int(timeOb[1])
        second = int(timeOb[2])

        date = datetime.datetime(year,month,day,hour,minute,second)
	return date

main()
